package cn.itsub.sodo.run;

import cn.itsub.sodo.core.Director;

public class Application {
	protected float _animationInterval = 1.0f/60;
	public boolean applicationDidFinishLaunching(){
		System.out.println("初始化........");
		return true;
	}
	public void setAnimationInterval(float interval){
		this._animationInterval = interval;
	}
	public void run(){
		// 首次启动调用初始化函数 
	    if (! applicationDidFinishLaunching()) 
	    { 
	        return ; 
	    }
	    
	    long iLastTime = System.currentTimeMillis();
	    
	    
	    Director director = Director.getInstance();
	 
	    // 游戏主循环，这里 Linux 的实现相比其它平台的实现，简单明了 
	    for (;;) {
	    	long inteval = (long) (_animationInterval*1000.0f);
	        long iCurTime = System.currentTimeMillis();
	        // 这里的几个时间变量，可以控制每一帧所运行的 最小 时间，从而控制游戏的帧率 
	        if (iCurTime - iLastTime >= inteval)
	        {
	        	iLastTime = iCurTime - (iCurTime % inteval);
	            director.mainLoop();
	        }
	        else
	        {
	            sleep(1);
	        }
	    }
	}
	
	private void sleep(long sp){
		try {
			Thread.sleep(sp);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	private static Application instance = null;
	public static Application getInstance(){
		if(instance==null){
			instance = new Application();
		}
		return instance;
	}
}
